package com.buyhouse.zhaimao;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.Toast;

/**
 * activity基类
 * 
 * @author zhouby
 *
 */
public abstract class BaseActivity extends FragmentActivity implements OnClickListener{

	/** 上下文 **/
	protected Activity mActivity;
	/** 吐司 **/
	protected Toast mToast;

	protected ProgressDialog bar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mActivity = this;
		MyApplication.getInstance().addActivity(this);
		Object o=getContentResView();
		if(o instanceof Integer){
			setContentView((Integer)o);
		}else if(o instanceof View){
			setContentView((View)o);
		}else{
			throw new IllegalStateException("getContentResView返回不是integer或者view类型");
		}
		initView();
		initData();
		getNetData();
	}

	
	/**获取view布局,只支持int和view*/
	public abstract Object getContentResView();
	/**初始化**/
	public abstract void initView();

	public abstract void initData();
	public abstract void getNetData();
	
	/**
	 * 获取demin
	 * 
	 * @param mRid
	 * @return
	 */
	protected int getDemonIntegerMethod(int mRid) {
		return (int) this.getResources().getDimension(mRid);
	}

	/**
	 * 取消toast
	 */
	public void toastDismis() {
		if (mToast != null) {
			mToast.cancel();
		}
	}

	/**
	 * 显示长Toast并且修改文字但不累加
	 */
	public void lToast(String msg) {
		showToast(msg, Toast.LENGTH_LONG);
	}

	/**
	 * 显示长Toast并且修改文字但不累加
	 */
	public void lToast(int msg) {
		showToast(msg, Toast.LENGTH_LONG);
	}

	/**
	 * 显示短Toast并且修改文字但不累加（一般情况调用此方法即可）
	 */
	public void sToast(String msg) {
		showToast(msg, Toast.LENGTH_SHORT);
	}

	/**
	 * 显示短Toast并且修改文字但不累加（一般情况调用此方法即可）
	 */
	public void sToast(int msg) {
		showToast(msg, Toast.LENGTH_SHORT);
	}

	/**
	 * 显示Toaset
	 * 
	 * @param msg
	 *            String
	 * @param duration
	 */
	private void showToast(String msg, int duration) {
		if (mToast == null) {
			mToast = Toast.makeText(mActivity, msg, duration);
		} else {
			mToast.setText(msg);
		}
		mToast.show();
	}

	/**
	 * 显示Toaset
	 * 
	 * @param msg
	 *            int
	 * @param duration
	 */
	private void showToast(int msg, int duration) {
		if (mToast == null) {
			mToast = Toast.makeText(mActivity, msg, duration);
		} else {
			mToast.setText(msg);
		}
		mToast.show();
	}

	/**
	 * 长Toast
	 */
	public void showLongToast(String msg) {
		Toast.makeText(mActivity, msg, Toast.LENGTH_LONG).show();
	}

	/**
	 * 短Toast
	 */
	public void showShortToast(String msg) {
		Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 显示自定义ProgressBar
	 */
	public void showLoading() {
		if(bar==null){
			bar=ProgressDialog.show(this, null, null);
			bar.setContentView(R.layout.include_loading);
		}
	}

//	/**
//	 * 自定义ProgressBar定义标题
//	 * 
//	 * @param s
//	 */
//	public void setLoadingText(String s) {
//		TextView v = (TextView) findViewById(R.id.tv_loading_text);
//		if (v != null)
//			v.setText(s);
//	}
	/**
	 * 显示默认ProgressBar
	 */
	public void showProgressDia(){
		if(bar==null){
			bar=ProgressDialog.show(this, null, null);
			bar.setContentView(new ProgressBar(this));
		}
	}
	/**
	 * 取消ProgressBar
	 */
	public void dismissProgressDia(){
		if(bar!=null&&bar.isShowing()){
			bar.dismiss();
			bar = null;
		}
	}

}
